package cn.otrue.patienthealthmanager.view;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.view.birthda.NumericWheelAdapteri;
import cn.otrue.patienthealthmanager.view.birthda.OnWheelChangedListeneri;
import cn.otrue.patienthealthmanager.view.birthda.OnWheelScrollListeneri;
import cn.otrue.patienthealthmanager.view.birthda.WheelViewi;

import com.mhealth37.open.sdk.MhealthApplication;

public class birthdaPopupWindow extends PopupWindow implements
		 OnClickListener {
	private MhealthApplication mhealthApp;
	private Context mContext;
	private View mPopView;
	private int textSize;
	private Activity activity;
	private boolean isComplete = false;
	private int mVersionId;
	public String desc;
	private static int START_YEAR = 1900, END_YEAR = 2014;
	public birthdaPopupWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public birthdaPopupWindow(Activity activity,
			final GetPickResultListener getPickResultListener) {

		super(activity);
		this.activity = activity;
		mContext = activity;
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPopView = inflater.inflate(R.layout.pick_birthday_layout,
				null);
		TextView tv_catt_cancle = (TextView) mPopView.findViewById(R.id.tv_catt_cancle);
		tv_catt_cancle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dismiss();
				
			}
		});
		TextView tv_catt_ok = (TextView) mPopView.findViewById(R.id.tv_catt_ok);
		
			tv_catt_ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					getPickResultListener.onGetResult(new Object[] { desc	 },2);
					
					
					dismiss();
				}
			});

	


		textSize = CommonUtil.dip2px(mContext, 20);

		initWindowStyleByVersion(mPopView);

		// 设置SelectPicPopupWindow的View
		this.setContentView(mPopView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		this.setOutsideTouchable(true);
		initPickBirthdayWindow(mPopView);
	}

	private void initWindowStyleByVersion(View mPopView2) {
		// 窗口标题容器
		RelativeLayout rl_catt_popTitle = (RelativeLayout) mPopView2
				.findViewById(R.id.rl_catt_popTitle);
		// 窗口标题
		TextView tv_catt_windowTitle = (TextView) mPopView2
				.findViewById(R.id.tv_catt_windowTitle);

		// int dip2px = dip2px(mContext, 18);
		rl_catt_popTitle.setBackgroundColor(mContext.getResources().getColor(
				R.color.window_bg_children));
		tv_catt_windowTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		// tv_catt_cancle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 初始化并准备日期选择控件
	 * 
	 * @param PopView
	 */
	private void initPickBirthdayWindow(View PopView) {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);

		String yearStr = year+"";
		String monthStr = month+1+"";
		String dayStr = day+"";

		// 如果是小于10,则显示为"0X"的样式
		String parten = "00";
		final DecimalFormat decimal = new DecimalFormat(parten);

		// 添加大小月月份并将其转换为list,方便之后的判断
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		// 年
		final WheelViewi wv_year = (WheelViewi) PopView.findViewById(R.id.year);
		wv_year.setAdapter(new NumericWheelAdapteri(START_YEAR, year));
		// 可循环滚动
		wv_year.setCyclic(true);
		// wv_year.setLabel("年");

		// 初始化时显示的数据
		if (!"".equals(yearStr)) {
			// wv_year.setCurrentItem(Integer.parseInt(yearStr) - END_YEAR - 1);
			wv_year.setCurrentItem(Integer.parseInt(yearStr) - START_YEAR);
		} else {

				wv_year.setCurrentItem(year - START_YEAR - 55);
			
		}

		// 月
		final WheelViewi wv_month = (WheelViewi) PopView.findViewById(R.id.month);
		wv_month.setAdapter(new NumericWheelAdapteri(1, 12));
		wv_month.setCyclic(true);
		// wv_month.setLabel("月");
		if (!"".equals(monthStr)) {
			wv_month.setCurrentItem(Integer.parseInt(monthStr) - 1);
			month = Integer.parseInt(monthStr) - 1;
		} else {
			wv_month.setCurrentItem(month);
		}

		// 日
		final WheelViewi wv_day = (WheelViewi) PopView.findViewById(R.id.day);
		wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapteri(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapteri(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapteri(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapteri(1, 28));
		}
		// wv_day.setLabel("日");
		if (!"".equals(dayStr)) {
			wv_day.setCurrentItem(Integer.parseInt(dayStr) - 1);
		} else {
			wv_day.setCurrentItem(day - 1);
		}

		// 根据屏幕密度来指定选择器字体的大小
		wv_day.TEXT_SIZE = textSize;
		wv_month.TEXT_SIZE = textSize;
		
		wv_year.TEXT_SIZE = textSize;

		// 添加"年"监听
		OnWheelChangedListeneri wheelListener_year = new OnWheelChangedListeneri() {
			public void onChanged(WheelViewi wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big
						.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapteri(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapteri(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapteri(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapteri(1, 28));
				}
			}

		
		};
		// 添加"月"监听
		OnWheelChangedListeneri wheelListener_month = new OnWheelChangedListeneri() {
			public void onChanged(WheelViewi wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapteri(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapteri(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapteri(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapteri(1, 28));
				}
			}
		};

		// 三个条目的滑动监听
		OnWheelScrollListeneri listener = new OnWheelScrollListeneri() {
			@Override
			public void onScrollingStarted(WheelViewi wheel) {
			}

			@Override
			public void onScrollingFinished(WheelViewi wheel) {
				// 把取得的数据交给desc
				desc = wv_year.getCurrentItem() + START_YEAR + "-"
						+ decimal.format(wv_month.getCurrentItem() + 1) + "-"
						+ decimal.format(wv_day.getCurrentItem() + 1);
			}

		
		};

		wv_year.addChangingListener(wheelListener_year);
		wv_year.addScrollingListener(listener);
		wv_month.addChangingListener(wheelListener_month);
		wv_month.addScrollingListener(listener);
		wv_day.addScrollingListener(listener);

		// 把取得的数据交给desc
		desc = wv_year.getCurrentItem() + START_YEAR + "-"
				+ decimal.format(wv_month.getCurrentItem() + 1) + "-"
				+ decimal.format(wv_day.getCurrentItem() + 1);
	}

}